package com.linzm.leetcode.primary.栈和队列;

import org.junit.Test;

import java.util.Stack;

/**
 * @Author zimingl
 * @Date 2022/10/7 12:16
 * @Description: TODO
 */
public class 栈和队列150逆波兰表达式求值 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();

        for (String str: tokens) {
            if (str.equals("+")){
                stack.push(stack.pop() + stack.pop());
            } else if (str.equals("-")) {
                stack.push(-stack.pop() + stack.pop());
            } else if (str.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else if (str.equals("/")) {
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 / temp1);
            } else {
                stack.push(Integer.valueOf(str));
            }
        }

        return stack.pop();
    }

    @Test
    public void test(){
        String[] tokens = {"2","1","+","3","*"};
        String[] s = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        int i = evalRPN(s);
        System.out.println(i);
    }
}
